<template>
  <div>
    <headNav title="" :isHome="isHome" :signIn="signIn" ></headNav>
    <div class="flexHeight bigDivCss" >
      <div @click="addClick" >
        <span>通过班级号添加孩子</span>
        <img src="@/assets/img/my/addClass.png" alt="" >
      </div>
      <div style="margin-top: 0.5rem;" @click="scanCodeClick" >
        <span>通过扫班级码添加孩子</span>
        <img src="@/assets/img/my/addQRcode.png" alt="" >
      </div>
    </div>
		<!-- {{text}} -->
    <div class="flexHeight bottomCss" >
      <span>提示：班级号与班级码可通过班主任获取</span>
    </div>
  </div>
</template>

<script>
import headNav from '@/components/headNav/headNav.vue'
import { mapActions, mapState, mapGetters, mapMutations } from 'vuex'
import { getChildRecordsApi, getClassNumberApi } from '@/api/myNewApi'
export default {
  components: {
    headNav,
  },
  computed: {
    ...mapState({
      studentId: state => state.user.studentId,
      roleCode: state => state.user.roleCode
    })
  },
  data () {
    return {
      isHome: false,
      signIn: false,
			text: '1111111'
    }
  },
  methods: {
    addClick () {
      localStorage.localQRCode = ''
      this.$router.push('addChildren')
    },
    // 点击扫码识别触发事件
    scanCodeClick() {
      let _this = this
      wx.scanQRCode({
        needResult: 1,   // 默认为0，扫描结果由微信处理。 1则直接返回扫描结果
        scanType: ["qrCode", "barCode"],  // 可以指定扫二维码还是一维码
        async success(res) {
          let arr = ''
          if (res.resultStr.indexOf('=') != -1 && res.resultStr.indexOf('&') != -1) {
            let arr1 = res.resultStr.split('=')  // ?a=1&b=2
            let arr2 = arr1[1].split('&')
            arr = arr2[0]
          } else {
            arr = res.resultStr
          }


          localStorage.editChildren = ''
          let params = {
            qrCode: arr
          }
          let val = await getClassNumberApi(params)
          if (!val) {
            _this.$vux.toast.text('识别的班级码不存在', 'middle')
          } else {
            localStorage.localQRCode = arr
            _this.$router.push('addChildren')
          }
          // _this.text = res.resultStr
        }
      })

    }
  },
  async mounted() {
    // let val = 'https://t-busi.xueerqin.net/study/index.html?qrcode=B41D5J&inviteUserId=&role=parent#/signUp/parentCourse'.split('=')
    // console.log(val[1].split('&')[0])
    let res = await getChildRecordsApi()
    if (res.length == 0) {
      this.isHome = false
      this.signIn = true
      return
    } else {
      for (let val of res) {
        if (val.statusDesc == '已绑定') {
          this.isHome = true
          this.signIn = false
          return
        } else {
          this.isHome = false
          this.signIn = true
        }
      }
    }
  }
}
</script>

<style lang="less" scoped >
.bottomCss {
  justify-content: center;
  align-items: center;
  font-size: 0.24rem;
  color: #999999;
}

.bigDivCss {
  justify-content: center;
  align-items: center;
  font-size: 0.36rem;
  color: #FFF;
  div {
    position: relative;
    margin-top: 1.5rem;
  }
  span {
    position: absolute;
    top: 0.45rem;
    left: 1.5rem;
  }
  img {
    width: 6.3rem;
  }
}

.flexCenter {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flexHeight {
  display: flex;
  flex-direction: column;
}

.flexAlign {
  display: flex;
  align-items: center;
}

.flexEdge {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
